package com.gitee.simons.oauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.simons.oauth.entity.dto.SysUserDetails;
import com.gitee.simons.oauth.entity.po.SysUser;
import com.gitee.simons.oauth.mapper.SysRoleMapper;
import com.gitee.simons.oauth.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author simon jiang
 * @time 2020/7/7 15:02
 */
@Service
@Slf4j
public class UserServiceImpl implements UserDetailsService {


    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
        List<String> roleCodes = sysRoleMapper.getRoleCodesByUsername(username);
        return new SysUserDetails(sysUser.getPassword(), sysUser.getUsername(), roleCodes);
    }
}
